package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeApplyDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeApproveDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeBackDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeStockDTO;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeApproveService;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeBackService;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeService;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeStockService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "备件领用流程管理")
@RestController
@RequestMapping(value = "/api/w/spareconsume")
public class SpareConsumeController {

    @Autowired
    private SpareConsumeService spareConsumeService;

    @Autowired
    private SpareConsumeApproveService spareConsumeApproveService;

    @Autowired
    private SpareConsumeStockService spareConsumeStockService;

    @Autowired
    private SpareConsumeBackService spareConsumeBackService;

    @ApiOperation("新增备品备件申请表单")
    @PostMapping("/applyform")
    public ResultDTO<SpareConsumeApplyDTO> create(@RequestBody final SpareConsumeApplyDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeService.create(dto, user));
    }

    @ApiOperation("查看备品备件申请表单")
    @GetMapping("/applyform/{workNum}/{taskId}")
    public ResultDTO<SpareConsumeApplyDTO> details(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增组长审批意见")
    @PostMapping("/leaderapproval")
    public ResultDTO<SpareConsumeApproveDTO> create(@RequestBody final SpareConsumeApproveDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeApproveService.create(dto, user));
    }

    @ApiOperation("查看备品备件审批意见")
    @GetMapping("/leaderapproval/{workNum}/{taskId}")
    public ResultDTO<SpareConsumeApproveDTO> approveDetails(@PathVariable final String workNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeApproveService.detail(workNum, taskId, user));
    }

    @ApiOperation("新增选择备件")
    @PostMapping("/stockselected")
    public ResultDTO<List<SpareConsumeStockDTO>> create(@RequestBody final List<SpareConsumeStockDTO> dtos,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeStockService.create(dtos, user));
    }

    @ApiOperation("获取确认人")
    @GetMapping("/confirmUser/{workOrderNum}")
    public ResultDTO<String> listConfirmUser(@PathVariable final String workOrderNum) {
        return ResultDTO.success(this.spareConsumeService.searchConfirmUser(workOrderNum));
    }

    @ApiOperation("选择备品备件")
    @GetMapping("/liststock")
    public PageResultDTO<SpareConsumeStockDTO> listPage(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.spareConsumeStockService.search(pageable, searchable));
    }

    @ApiOperation("根据故障工单号查询已经申请完的备品备件")
    @GetMapping("/liststock/{workNum}/finish")
    public ResultListDTO<StockDTO> finishList(@PathVariable final String workNum) {
        return ResultListDTO.success(this.spareConsumeStockService.finishList(workNum));
    }

    @ApiOperation("根据故障工单号查询已经申请完的备品备件")
    @GetMapping("/liststock/{workNum}/finish/{confirmUser}")
    public ResultListDTO<StockDTO> finishList(@PathVariable final String workNum,
            @PathVariable final String confirmUser) {
        return ResultListDTO.success(this.spareConsumeStockService.finishList(workNum, confirmUser));
    }

    @ApiOperation("新增退废利旧")
    @PostMapping("/backsave")
    public ResultDTO<List<SpareConsumeBackDTO>> createBack(@RequestBody final List<SpareConsumeBackDTO> dtos,
            final UserBean user) {
        return ResultDTO.success(this.spareConsumeBackService.create(dtos, user));
    }

    @ApiOperation("选择退废利旧")
    @GetMapping("/listback")
    public PageResultDTO<SpareConsumeBackDTO> listBackPage(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.spareConsumeBackService.search(pageable, searchable));
    }

}
